<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>系统字典维护</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/layuimini/lib/layui-v2.5.5/css/layui.css}"
          href="../../../static/layuimini/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" th:href="@{/layuimini/css/public.css}" href="../../../static/layuimini/css/public.css"
          media="all">
    <style>
        body {
            background-color: #fff;
            padding: 0;
            margin: 0;
        }

        body .layui-layer-btn {
            background-color: #F8F8F8;
            border-top: 1px solid #eee;
            padding-top: 10px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <form class="layui-form" lay-filter="tableToolForm">
            <div class="layui-form-item">
                <div class="layui-inline">
                    搜索类型
                </div>
                <div class="layui-inline">
                    <select id="searchType" name="searchType" lay-filter="searchType">
                        <option value="1">配置键</option>
                        <option value="2">配置值</option>
                        <option value="3">描述</option>
                    </select>
                </div>
                <div class="layui-inline">
                    <input id="searchStr" class="layui-input" name="searchStr" type="text" placeholder="输入关键字">
                </div>
                <div class="layui-inline">
                    <select id="configGroup" name="configGroup" lay-filter="configGroup">
                        <option value="">全部配置组</option>
                        <option th:each="configGroup:${configGroupList}" th:value="${configGroup}"
                                th:text="${configGroup}" value="1">类别名称
                        </option>
                    </select>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn" id="btn-search">
                        <i class="layui-icon">&#xe615;</i>搜索
                    </button>
                    <button class="layui-btn layui-btn-primary" id="btn-reload">
                        <i class="layui-icon">&#xe666;</i>清空搜索
                    </button>
                    <button class="layui-btn layui-btn-normal" id="btn-add">
                        <i class="layui-icon">&#xe624;</i>新增
                    </button>
                    <!--<button class="layui-btn layui-btn-danger" id="btn-delete">
                        <i class="layui-icon">&#xe640;</i>删除
                    </button>-->
                </div>
            </div>
        </form>

        <table id="sysConfigTable" lay-filter="sysConfigTableFilter"></table>


    </div>
</div>
<script th:src="@{/layuimini/lib/layui-v2.5.5/layui.js}" src="../../../static/layuimini/lib/layui-v2.5.5/layui.js"
        charset="utf-8"></script>
<script th:src="@{/layuimini/js/lay-config.js?v=1.0.4}" src="../../../static/layuimini/js/lay-config.js?v=1.0.4"
        charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        var insTb = table.render({
            elem: '#sysConfigTable',
            url: '[[@{/}]]manage/config/list',
            cols: [
                [
                    {type: 'checkbox', minWidth: 50},
                    {field: 'configId', title: 'ID', minWidth: 50, width: 50},
                    {field: 'configGroup', title: '配置组', minWidth: 120},
                    {field: 'configKey', title: '配置键', minWidth: 120},
                    {field: 'configValue', title: '配置值', minWidth: 120},
                    {field: 'configDesc', title: '描述', minWidth: 120},
                    {
                        title: '操作', minWidth: 210, width: 210, align: "right", templet: function (d) {
                            var btn = '';
                            btn += '<button class="layui-btn layui-btn-xs  layui-btn-radius" lay-event="edit"><i class="layui-icon">&#xe642;</i>编辑</button>';
                            return btn;
                        }
                    }
                ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });

        form.on('submit(tableToolForm)', function (data) {
            return false;
        });

        //搜索
        $('#btn-search').click(function () {
            var searchType = $('#searchType').val();
            var searchStr = $('#searchStr').val();
            var configGroup = $('#configGroup').val();

            table.reload('sysConfigTable', {
                where: { //设定异步数据接口的额外参数，任意设
                    searchType: searchType
                    , searchStr: searchStr
                    , configGroup: configGroup
                }
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
                , done: function () {
                    this.where = {};//清空查询参数
                }
            }); //只重载数据
        });
        //清空搜索
        $('#btn-reload').click(function () {
            $('#searchStr').val('');
            table.reload('currentTableId', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            }); //只重载数据
        });
        //新增
        $('#btn-add').click(function () {
            layer.open({
                type: 2
                , title: '新增配置'
                , content: '[[@{/}]]manage/config/config-edit.html'
                , resize: false
                , area: ['500px', '340px']
                , btn: ['保存', '取消']
                , yes: function (index) {
                    var iframeWindow = window['layui-layer-iframe' + index];
                    var editWindow = $(iframeWindow.document);
                    if (editWindow.find('#configGroup').val().trim().length < 1) {
                        layer.msg('配置组不能为空！');
                        return;
                    }
                    if (editWindow.find('#configKey').val().trim().length < 1) {
                        layer.msg('配置键不能为空！');
                        return;
                    }
                    if (editWindow.find('#configValue').val().trim().length < 1) {
                        layer.msg('配置值不能为空！');
                        return;
                    }
                    var postData = editWindow.find('#configForm').serialize();
                    layer.load(2);
                    $.ajax({
                        type: 'post',
                        url: '[[@{/}]]manage/config/configSave',
                        data: postData,
                        dataType: 'json',
                        success: function (result) {
                            console.log(result);
                            if (result.success) {
                                layer.msg("操作成功");
                                editWindow.find('#configId').val(result.data.configId);
                            } else {
                                layer.msg(result.msg);
                            }
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            layer.msg(textStatus);
                        },
                        complete: function () {
                            layer.closeAll('loading');
                        }
                    });
                }
                , end: function () {
                    insTb.reload();
                }
            });
        });

        table.on('tool(sysConfigTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                layer.open({
                    type: 2
                    , title: '修改配置 [id:' + data.configId + ']'
                    , content: '[[@{/}]]manage/config/config-edit.html?configId=' + data.configId
                    , resize: false
                    , area: ['500px', '340px']
                    , btn: ['保存', '取消']
                    , yes: function (index) {
                        var iframeWindow = window['layui-layer-iframe' + index];
                        var editWindow = $(iframeWindow.document);
                        if (editWindow.find('#configGroup').val().trim().length < 1) {
                            layer.msg('配置组不能为空！');
                            return;
                        }
                        if (editWindow.find('#configKey').val().trim().length < 1) {
                            layer.msg('配置键不能为空！');
                            return;
                        }
                        if (editWindow.find('#configValue').val().trim().length < 1) {
                            layer.msg('配置值不能为空！');
                            return;
                        }
                        var postData = editWindow.find('#configForm').serialize();
                        layer.load(2);
                        $.ajax({
                            type: 'post',
                            url: '[[@{/}]]manage/config/configSave',
                            data: postData,
                            dataType: 'json',
                            success: function (result) {
                                console.log(result);
                                if (result.success) {
                                    layer.msg("操作成功");
                                } else {
                                    layer.msg(result.msg);
                                }
                            },
                            error: function (jqXHR, textStatus, errorThrown) {
                                layer.msg(textStatus);
                            },
                            complete: function () {
                                layer.closeAll('loading');
                            }
                        });
                    }
                    , end: function () {
                        insTb.reload();
                    }
                });
            }
        });

    });
</script>

</body>
</html>